import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:pcp/common/config/config.dart';
import 'package:pcp/main.dart';
import 'package:pcp/pages/mine_file_page.dart';
import 'package:pcp/pages/mine_workstation_page.dart';
import 'package:pcp/widgets/mine/contact_item.dart';
import 'package:pcp/widgets/mine/menu_item.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MinePage extends StatefulWidget {
  @override
  _MinePageState createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> with AutomaticKeepAliveClientMixin<MinePage> {
  final double _appBarHeight = 180.0;

  List userFile = new List();
  String userName = "";
  String teamName = "";
  var projectCount, reviewCount, collectionsCount;

  @override
  bool get wantKeepAlive => true;

  Future<int> getUser() async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String userId = prefs.getString("userId");
      userName = prefs.getString("userName");
//      print(userId);
      Response response = await Dio().post(
          Config.BASE_URL + "/user/getUser",
          data: {"_id": userId});
      Response response2 = await Dio().get(
          Config.BASE_URL + "/project/getProjectCount",
          queryParameters: {"userId": userId});
      userFile = response.data;
      teamName = userFile[0]["teamName"];
      projectCount = response2.data["projectCount"].toString();
      reviewCount = response2.data["reviewCount"].toString();
      collectionsCount = response2.data["collectionsCount"].toString();
      return 1;
    } catch (e) {
      print(e);
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getUser().then((data) {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: new Color.fromARGB(255, 242, 242, 245),
      body: new CustomScrollView(
        slivers: <Widget>[
          new SliverAppBar(
            expandedHeight: _appBarHeight,
            flexibleSpace: new FlexibleSpaceBar(
              collapseMode: CollapseMode.parallax,
              background: new Stack(
                fit: StackFit.expand,
                children: <Widget>[
                  const DecoratedBox(
                    decoration: const BoxDecoration(
                      gradient: const LinearGradient(
                        begin: const Alignment(0.0, -1.0),
                        end: const Alignment(0.0, -0.4),
                        colors: const <Color>[
                          const Color(0x00000000),
                          const Color(0x00000000)
                        ],
                      ),
                    ),
                  ),
                  new Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      new Expanded(
                        flex: 3,
                        child: new Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            new Padding(
                              padding: const EdgeInsets.only(
                                top: 30.0,
                                left: 30.0,
                                bottom: 15.0,
                              ),
                              child: new Text(
                                userName,
                                style: new TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 35.0),
                              ),
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(
                                left: 30.0,
                              ),
                              child: new Text(
                                teamName,
                                style: new TextStyle(
                                    color: Colors.white, fontSize: 15.0),
                              ),
                            ),
                          ],
                        ),
                      ),
                      new Expanded(
                        flex: 1,
                        child: new Padding(
                          padding: const EdgeInsets.only(
                            top: 40.0,
                            right: 30.0,
                          ),
                          child: new CircleAvatar(
                            radius: 35.0,
                            backgroundImage: new NetworkImage(
                              Config.BASE_URL + "/user/getProfilePhoto?sex=male",
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ],
              ),
            ),
          ),
          new SliverList(
            delegate: new SliverChildListDelegate(
              <Widget>[
                new Container(
                  color: Colors.white,
                  child: new Padding(
                    padding: const EdgeInsets.only(
                      top: 10.0,
                      bottom: 10.0,
                    ),
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        new ContactItem(
                          count: projectCount ?? "0",
                          title: '项目数',
                        ),
                        new ContactItem(
                          count: reviewCount ?? "0",
                          title: '总任务数',
                        ),
                        new ContactItem(
                          count: collectionsCount ?? "0",
                          title: '总收藏数',
                        ),
                      ],
                    ),
                  ),
                ),
                new Container(
                  color: Colors.white,
                  margin: const EdgeInsets.only(top: 10.0),
                  child: Column(
                    children: <Widget>[
                      new MenuItem(
                        icon: Icons.perm_media,
                        title: '我的工作站',
                        onPressed: () {
                          Navigator.of(context).push(new MaterialPageRoute(
                              builder: (BuildContext context) => new MineWorkStationPage()));
                        },
                      ),
                      new MenuItem(
                        icon: Icons.account_box,
                        title: '我的资料',
                        onPressed: () {
                          Navigator.of(context).push(new MaterialPageRoute(
                              builder: (BuildContext context) => new MineFilePage()));
                        },
                      ),
                      new MenuItem(
                        icon: Icons.security,
                        title: '设置',
                      ),
                    ],
                  ),
                ),
                new Container(
                  color: Colors.white,
                  margin: const EdgeInsets.only(top: 10.0),
                  child: new MenuItem(
                    icon: Icons.logout,
                    title: '退出登录',
                    onPressed: () async {
                      SharedPreferences prefs = await SharedPreferences.getInstance();
                      prefs.clear();
                      Navigator.of(context).push(new MaterialPageRoute(
                          builder: (BuildContext context) => new MainPage(userName: "请登录",)));
                    },
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
//flutter run --enable-software-rendering
